#下载当天日志
function getLog(){
	#如果文件已经存在就删除
	if [ -f "./${1}-all-${2}" ];then
		rm -f ./${1}-all-${2}
	fi
	for str in $(kubectl get pod -n msp-prod | grep ${1});do
		if [[ $str == ${1}* ]];then
			echo "$str"
			kubectl cp msp-prod/$str:logs/prod/${1}/${2} ./${str}-${2}
			#合并日志
			#如果需要限制合并行数
			if [[ -n ${3} ]];then
				tail -n ${3} ${str}-${2} >> ${1}-all-${2}
			else
				less ${str}-${2} >> ${1}-all-${2}
			fi
			echo "=====================================================================================${str}-${2}" >> ${1}-all-${2}
		fi
	done
#    这种写法更简单
#    kubectl get po -n msp-prod | grep $project_name | awk '{print $1}' |
#    while read line; 
#        do kubectl cp -n msp-prod ${line}:logs/prod/$project_name/$log_level.log ${line}.log ; 
#        echo "输出日志：${line}.log"
#    done
}

#通过logs来获取日志
#下载当天日志
function getLogLogs(){
	for str in $(kubectl get pod -n msp-prod | grep ${1});do
		if [[ $str == ${1}* ]];then
			echo "$str"
			kubectl logs $str -n msp-prod >> ${1}-logs.log
			echo "=====================================================================================${str}" >> ${1}-logs.log
		fi
	done
}


#下载往日日志
function getLogBefore(){
	#如果文件已经存在就删除
	if [ -f "./${1}-all-${2%.gz}" ];then
		rm -f ./${1}-all-${2%.gz}
	fi
	for str in $(kubectl get pod -n msp-prod | grep ${1});do
		if [[ $str == ${1}* ]];then
			echo "$str"
			kubectl cp msp-prod/$str:logs/prod/${1}/${2} ./${str}-${2}
			#强制自动解压
			gzip -df ${str}-${2}
			#如果需要限制合并行数
			if [[ -n ${3} ]];then
				tail -n ${3} ${str}-${2%.gz} >> ${1}-all-${2%.gz}
			else
				less ${str}-${2%.gz} >> ${1}-all-${2%.gz}
			fi
			echo "=====================================================================================${str}-${2%.gz}" >> ${1}-all-${2%.gz}
		fi
	done
}

#下载全部日志
function getAllLog(){
	for str in $(kubectl get pod -n msp-prod | grep ${1});do
		if [[ $str == ${1}* ]];then
			echo "$str"
			kubectl cp msp-prod/$str:logs/prod/${1} ./${str}
		fi
	done
}

#下载jar包
function getJar(){
	for str in $(kubectl get pod -n msp-prod | grep ${1});do
		if [[ $str == ${1}* ]];then
			echo "$str"
			kubectl cp msp-prod/$str:app.jar ./${1}.jar
			break
		fi
	done
}


#获取服务名称
function getServiceName() {
	serviceName=$1
	if [[ $1 =~ cmsc-ms-res || $1 == cmr ]];then
		serviceName=cmsc-ms-resourceservice
	elif [[ $1 =~ coms-ms-c || $1 == cmc ]];then
		serviceName=coms-ms-cmdbservice
	elif [[ $1 =~ cms-ms-a || $1 == cma ]];then
		serviceName=cms-ms-adapter
	fi
	echo ${serviceName}
}

#获取日志名称
function getLogName() {
	logName=$1
	if [[ $1 == i* ]];then
		logName=info.log
	elif [[ $1 == e* ]];then
		logName=error.log
	elif [[ $1 == w* ]];then
		logName=warn.log
	elif [[ $1 == d* ]];then
		logName=debug.log
	fi
	echo ${logName}
}

#获取往期日志名称
function getLogNameBefore() {
	logName=$1
	if [[ $1 == i* ]];then
		logName=info-${2}.0.log.gz
	elif [[ $1 == e* ]];then
		logName=error-${2}.0.log.gz
	elif [[ $1 == w* ]];then
		logName=warn-${2}.0.log.gz
	elif [[ $1 == d* ]];then
		logName=debug-${2}.0.log.gz
	fi
	echo ${logName}
}

#--------------------------------------------------------
#获取当天的日志
if [[ -n ${1} ]] && [[ ${2} == i* || ${2} == e* || ${2} = w* || ${2} == d* ]] && [[ -z ${3} ]];then
	#获取当天日志
	echo "开始下载日志: "logs/prod/$(getServiceName ${1})/$(getLogName ${2})
	getLog $(getServiceName ${1}) $(getLogName ${2})
fi

#获取当天的日志通过logs
if [[ -n ${1} ]] && [[ ${2} == logs ]];then
	#获取当天日志
	echo "开始下载日志: "logs/prod/$(getServiceName ${1})
	getLogLogs $(getServiceName ${1})
fi

#行数合并日志
re='^[+-]?[0-9]+([.][0-9]+)?$'
if [[ -n ${1} ]] && [[ ${2} == i* || ${2} == e* || ${2} = w* || ${2} == d* ]] && [[ ${3} =~ $re ]];then
	#获取当天日志
	echo "开始下载日志: "logs/prod/$(getServiceName ${1})/$(getLogName ${2})
	getLog $(getServiceName ${1}) $(getLogName ${2}) ${3}
fi

#下载往期日志
date_regex="^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
if [[ -n ${1} ]] && [[ ${2} == i* || ${2} == e* || ${2} = w* || ${2} == d* ]] && [[ ${3} =~ $date_regex ]] && [[ -z ${4} ]];then
	#获取当天日志
	echo "开始下载日志: "logs/prod/$(getServiceName ${1})/$(getLogNameBefore ${2} ${3})
	getLogBefore $(getServiceName ${1}) $(getLogNameBefore ${2} ${3})
fi

#行数合并往期日志
if [[ -n ${1} ]] && [[ ${2} == i* || ${2} == e* || ${2} = w* || ${2} == d* ]] && [[ ${3} =~ $date_regex ]] && [[ ${4} =~ $re ]];then
	#获取当天日志
	echo "按行数开始下载日志: "logs/prod/$(getServiceName ${1})/$(getLogNameBefore ${2} ${3})
	getLogBefore $(getServiceName ${1}) $(getLogNameBefore ${2} ${3}) ${4}
fi

#直接下载全部日志
if [[ -n ${1} && ${2} == all ]];then
	echo "开始下载日志: "logs/prod/$(getServiceName ${1})
	getAllLog $(getServiceName ${1})
fi

#获取jar包	
if [[ -n ${1} && ${2} == jar ]];then
	getJar $(getServiceName ${1})
fi

#删除pod重启
if [[ -n ${1} && ${2} == rpod ]];then
	kubectl get po -n msp-prod | grep $(getServiceName ${1}) | awk '{print $1}' | while read line; do kubectl delete po ${line} -n msp-prod ; done
fi

#查看版本
if [[ ${1} == v* && -z ${2} ]];then
	helm list -n msp-prod | grep cmp
fi

#删除远程镜像
if [[ ${1} == a* && -n ${2} ]];then
	ansible all -i /etc/kubeos/multinode -m shell -a "crictl rmi ${2}"
fi


if [[ ${1} == deploy && -n ${2} ]];then
  #推送镜像和删除远程仓库镜像
  docker push ${2}
  #删除远程镜像原来的数据
  ansible all -i /etc/kubeos/multinode -m shell -a "crictl rmi ${2}"
  #重启pod
  name=$(echo ${2} | awk -F "/" '{print $4}')
  delete_pod=$(echo ${name} | awk -F ":" '{print $1}')
  kubectl get po -n msp-prod | grep ${delete_pod} | awk '{print $1}' | while read line; do kubectl delete po ${line} -n msp-prod ; done
fi









